package com.veo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.veo.entity.CommonResult;
import com.veo.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class RateLimitController {

    @GetMapping("/byResource")
    //代码耦合，业务逻辑和异常逻辑混合
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称限流测试",new Payment(2021L,"serial001"));
    }


    public CommonResult handleException(BlockException exception){
        return new CommonResult(4444,exception.getClass().getCanonicalName()+"\t"+"资源不可用！");
    }


    @GetMapping("/rateLimit/customerBlockHandle")
    @SentinelResource(value = "customerBlockHandle",blockHandlerClass = CustomerBlockHandle.class,
            blockHandler = "handleException2")
    //必须为静态方法否则报错
    public CommonResult rateLimit(){
        return new CommonResult(200,"按客户自定义",new Payment(2021L,"serial001"));
    }
}
